package io.github.uicdb.badskill.block;

import io.github.uicdb.badskill.BSRegistries;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.Component;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class DisableBlock extends Block implements EntityBlock {
    public DisableBlock(Properties properties) {
        super(properties);
    }


    @Nullable
    @Override
    public BlockEntity newBlockEntity(@NotNull BlockPos p_153215_, @NotNull BlockState p_153216_) {
        return BSRegistries.BLOCKENTITY_DISABLE.get().create(p_153215_, p_153216_);
    }

    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(@NotNull Level level, @NotNull BlockState blockState, @NotNull BlockEntityType<T> entityType) {
        return (level1, pos, p_155255_, p_155256_) -> level1.getBlockEntity(pos, BSRegistries.BLOCKENTITY_DISABLE.get()).orElseThrow().tick(level1);
    }

    @Override
    public void appendHoverText(ItemStack p_49816_, @Nullable BlockGetter p_49817_, List<Component> components, TooltipFlag p_49819_) {
        super.appendHoverText(p_49816_, p_49817_, components, p_49819_);
        components.add(Component.translatable("tip.badskill.disable.block"));
    }

    @Override
    public void animateTick(BlockState p_220827_, Level level, BlockPos pos, RandomSource randomSource) {
        super.animateTick(p_220827_, level, pos, randomSource);
        if (level.isClientSide) {
            if (Minecraft.getInstance().player.getMainHandItem().is(asItem()))
                level.addParticle(ParticleTypes.LAVA, true, pos.getX() + randomSource.nextDouble(), pos.getY() + 0.5f, pos.getZ() + randomSource.nextDouble(), 0, -1, 0);
        }
    }
}
